<?php

$loader->requireOnce('includes/LabResult/LabResultEngine.class.php');
/**
 * Processor for lab result
 * @package modules.dept_laboratory.includes
 * @author viethd
 * @version $Id$
 * @copyright 2009 LHV Company
 */
class LabResultProcessor {
	private $logger ;
	private $controller;
	private $labResultEngine;
	function __construct($controller) {
		$this->logger = LoggerManager::getLogger('module.dept_laboratory.includes.LabResult.LabResultProcessor');
		$this->controller = $controller;
		$this->labResultEngine = new LabResultEngine();
	}



	public function renderSelectorHtml($patientId, $selectedLabAssignmentTicketId, $selectedLabTicketId) {
		$labAssignmentTicketObj = Celini::newOrdo('LabAssignmentTicket');
		$listOfAssignment = $labAssignmentTicketObj->listAssignmentsByPId($patientId,
			Array(
				EnumerationConstant::LAB_ASSIGNMENT_TICKET_INPROGRESS,
			));
		//build labAssignment select
		$labAssignTicketArr = array();
		foreach ($listOfAssignment as $assignment) {
			$text = L10nHelper::renderDateTime($assignment['request_time'],'short', 'datetime');
			$labAssignTicketArr[$assignment['lab_assignment_ticket_id']] = $text;
		}
		$this->controller->assign('selectedlabAssignmentTicketId', $selectedLabAssignmentTicketId);
		$this->controller->assign('selectedLabTicketId', $selectedLabTicketId);
		$this->controller->assign('labAssignmentTicketList', $labAssignTicketArr);
		$this->controller->assign('labTicketList', $labTicketArr);
		return $this->controller->view->render('lab_result_laselect.html');
	}

	public function renderSelectorHtml4View($ticketId, $selectedLabAssignmentTicketId, $selectedLabTicketId, $enumStatus) {
		$labAssignmentTicketObj = Celini::newOrdo('LabAssignmentTicket');
		$listOfAssignment = $labAssignmentTicketObj->listAssignmentsByTicketId($ticketId,
			Array(
            EnumerationConstant::LAB_ASSIGNMENT_TICKET_ASSIGNED,
            EnumerationConstant::LAB_ASSIGNMENT_TICKET_WAITING,
            EnumerationConstant::LAB_ASSIGNMENT_TICKET_INPROGRESS,
            EnumerationConstant::LAB_ASSIGNMENT_TICKET_DONE        
            )
		);
			
		//build labAssignment select
		$labAssignTicketArr = array();
		foreach ($listOfAssignment as $assignment) {
			$text = L10nHelper::renderDateTime($assignment['request_time'],'short', 'datetime')
			. ' [' . Utils::getEnumTextByKey(EnumerationConstant::LAB_ASSIGNMENT_TICKET_STATUS, $assignment['enum_status']) . ']';
			$labAssignTicketArr[$assignment['lab_assignment_ticket_id']] = $text;
		}
		$this->controller->assign('selectedlabAssignmentTicketId', $selectedLabAssignmentTicketId);
		$this->controller->assign('selectedLabTicketId', $selectedLabTicketId);
		$this->controller->assign('labAssignmentTicketList', $labAssignTicketArr);
		$this->controller->assign('labTicketList', $labTicketArr);
		return $this->controller->view->render('lab_result_laselect_view_only.html');
	}

	public function saveLabResult($selectedLabTicketId, $metaData, $resultData) {
		$labResultModel = $this->labResultEngine->getLabModel($selectedLabTicketId);
		$labResultModel->saveData($metaData, $resultData);

	}

	public function renderLabTicketSelectorHtml($selectedLabAssignmentTicketId, $selectedLabTicketId, $isInput=true) {
		$listOfLabTicketObj = Celini::newOrdo('LabTicket');
		$listOfLabTicket = $listOfLabTicketObj->listLabTickets($selectedLabAssignmentTicketId);

		if (($selectedLabTicketId == 0 || $selectedLabTicketId == '')
		&& count($listOfLabTicket) > 0 ) {
			$selectedLabTicketId = $listOfLabTicket[0]['lab_ticket_id'];
		}

		$labTicketArr = array();
		foreach ($listOfLabTicket as $labTicket) {
			$text = $labTicket['name'];
			if ($labTicket['is_done'] != 0) {
				$text .= ' [' . L10nHelper::_('Done') . ']';
			} else {
				$text .= ' [' . L10nHelper::_('No result') . ']';
			}
			$labTicketArr[$labTicket['lab_ticket_id']] = $text;
		}
		$this->controller->assign('selectedlabAssignmentTicketId', $selectedLabAssignmentTicketId);
		$this->controller->assign('selectedLabTicketId', $selectedLabTicketId);
		$this->controller->assign('labTicketList', $labTicketArr);
		if ($isInput == true) {
			return $this->controller->view->render('lab_result_lab_ticket_select.html');
		} else {
			return $this->controller->view->render('lab_result_lab_ticket_select_view_only.html');
		}
	}


	public function renderMetaData($labAssignmentTicketId, $labResultModel) {
		$labAssignmentTicketObj = Celini::newOrdo('LabAssignmentTicket');
		$labAssignmentTicket = $labAssignmentTicketObj->getAssignmentInfo($labAssignmentTicketId);
		$employeeObj = Celini::newORDO("Employee");
		$techList = $employeeObj->getEmployeeOfDepartment($this->controller->_me->get_department_id(), false, true);
		$labTicket = $labResultModel->getLabTicketOrdo()->getLabTicket();

		$this->controller->assign('treatmentDoctorName', '');
		$this->controller->assign('lastUpdatedTime', '');
		$this->controller->assign('techList', '');
		$this->controller->assign('labTicketName', '');
		$this->controller->assign('technicianId1', '');
		$this->controller->assign('technicianId2', '');
		$this->controller->assign('machine', '');
		$this->controller->assign('sample', '');
		$this->controller->assign('diagnosis', '');
		if ($labAssignmentTicket != null) {

			$this->controller->assign('treatmentDoctorName', $labAssignmentTicket['treatment_doctor_name']);
			$this->controller->assign('executedTime', '');
			$this->controller->assign('techList', $techList);
			if ($labTicket != null) {
				$this->controller->assign('labTicketName', $labTicket['name']);
				$this->controller->assign('modified', L10nHelper::renderDateTime($labTicket['modified'],'short', 'datetime'));
				$this->controller->assign('technicianId1', $labTicket['technician1_id']);
				$this->controller->assign('technicianId2', $labTicket['technician2_id']);
				$this->controller->assign('machine', $labTicket['machine_name']);
				$this->controller->assign('sample', '');
			}
			$this->controller->assign('diagnosis', $labAssignmentTicket['diagnosis']);

		}
		return $this->controller->view->render('lab_result_meta.html');

	}

	public function renderMetaData4View($labAssignmentTicketId, $labResultModel) {
		$labAssignmentTicketObj = Celini::newOrdo('LabAssignmentTicket');
		$labAssignmentTicket = $labAssignmentTicketObj->getAssignmentInfo($labAssignmentTicketId);
		$employeeObj = Celini::newORDO("Employee");
		$techList = $employeeObj->getEmployeeOfDepartment($this->controller->_me->get_department_id(), false, true);
		$labTicket = $labResultModel->getLabTicketOrdo()->getLabTicket();

		$this->controller->assign('treatmentDoctorName', '');
		$this->controller->assign('lastUpdatedTime', '');
		$this->controller->assign('techList', '');
		$this->controller->assign('labTicketName', '');
		$this->controller->assign('technicianId1', '');
		$this->controller->assign('technicianId2', '');
		$this->controller->assign('technician1Name', '');
		$this->controller->assign('technician2Name', '');
		$this->controller->assign('machine', '');
		$this->controller->assign('sample', '');
		$this->controller->assign('diagnosis', '');
		if ($labAssignmentTicket != null) {

			$this->controller->assign('treatmentDoctorName', $labAssignmentTicket['treatment_doctor_name']);
			$this->controller->assign('executedTime', '');
			$this->controller->assign('techList', $techList);
			if ($labTicket != null) {
				$this->controller->assign('labTicketName', $labTicket['name']);
				$this->controller->assign('modified', L10nHelper::renderDateTime($labTicket['modified'],'short', 'datetime'));
				$this->controller->assign('technicianId1', $labTicket['technician1_id']);
				$this->controller->assign('technicianId2', $labTicket['technician2_id']);
				$this->controller->assign('technician1Name', $labTicket['name1']);
				$this->controller->assign('technician2Name', $labTicket['name2']);
				$this->controller->assign('machine', $labTicket['machine_name']);
				$this->controller->assign('sample', '');
			}
			$this->controller->assign('diagnosis', $labAssignmentTicket['diagnosis']);

		}
		return $this->controller->view->render('lab_result_meta_view_only.html');

	}

	public function renderMetaData4Print($labAssignmentTicketId) {

		$labAssignmentTicketObj = Celini::newOrdo('LabAssignmentTicket');
		$labAssignmentTicket = $labAssignmentTicketObj->getAssignmentInfo($labAssignmentTicketId);
		$person = Celini::newORDO('Person', $labAssignmentTicket['patient_id']);
		$patient = Celini::newORDO('Patient', $labAssignmentTicket['patient_id']);
		$this->controller->assign('gender', $person->getGender());
		$this->controller->assign('address', $person->getAddress());
		$this->controller->assign('phone', $person->getPhoneNumber());
		$this->controller->assign('patientName', $person->getName());
		$this->controller->assign('patientId', $labAssignmentTicket['patient_id']);
		$this->controller->assign('patientCode', $patient->get('patient_code'));
		$this->controller->assign('patientDob', $person->getDateofBirth());
		$this->controller->assign('patientAge', $person->getAge());


		$this->controller->assign('treatmentDoctorName', '');
		$this->controller->assign('diagnosis', '');
		$this->controller->assign('requestTime', '');
		$this->controller->assign('treatmentDepartmentName', '');

		if ($labAssignmentTicket != null) {

			$this->controller->assign('treatmentDoctorName', $labAssignmentTicket['treatment_doctor_name']);
			$this->controller->assign('diagnosis', $labAssignmentTicket['diagnosis']);
			$this->controller->assign('requestTime', $labAssignmentTicket['request_time']);
			$this->controller->assign('treatmentDepartmentName', $labAssignmentTicket['treatment_department_name']);

		}
		return $this->controller->view->render('lab_result_meta_print.html');

	}

	public function renderResultInput($labAssignmentTicketId, $labResultModel, $isInput = true) {
		$labAssignedItemsModel = $labResultModel->getLabAssignedItemsModel();
		$labAssignmentTicketObj = Celini::newOrdo('LabAssignmentTicket');
		$labAssignmentTicketObj->setup($labAssignmentTicketId);
		$person = Celini::newORDO('Person', $labAssignmentTicketObj->get('patient_id'));

		$html = '';
		$html .= '<div style="height: 250px;" class="b_div">';
		$count = 1;
		$count1 = 1;
		$html .= '<table cellspacing="0" cellpadding="0" border="0" style="display: table;">';
		$html .= '<tbody>';
		foreach ($labAssignedItemsModel as $item) {

			$labItemsFactorModel = $item->getLabItemsFactorModel();
			//has one result
			if (count($labItemsFactorModel) == 1) {
				//reindex
				$arr = array_values($labItemsFactorModel);
				$fItem = $arr[0];
				$html .= '<tr>';
				if ($fItem->isNormal($person->getGender())) {
					$html .=   '<td style="width: 200px;">' . $count . '. ' . $item->getLabAssignedItemOrdo()->getNameText() . '</td>';
				} else {
					$html .=   '<td style="width: 200px;"><span style="color: red;">' . $count . '. ' . $item->getLabAssignedItemOrdo()->getNameText() . ' [*]</span></td>';
				}

				if ($isInput == true) {
					$html .=   '<td style="width: 100px;" onclick="editRow(\'' . $count1.'\')">';
				} else {
					$html .=   '<td style="width: 100px;" >';
				}
				$html .= '<span id="text_' . $count1 . '">' . $fItem->getResult() . '&nbsp;</span>';
				$html .= '<span id="edit_' . $count1 . '" style="display: none;"><input class="labItemResultInput" labresultid="' . $fItem->getLabResultId() . '" type="text" style="width: 90px;" value="' . $fItem->getResult() . '"/></span>';
				$html .= '</td>';
				$html .= '<td style="width: 65px;">' . $fItem->getUnit() . '&nbsp;</td>';
				$html .= '<td style="width: 120px;">' . $fItem->getNormalResultMale() . '&nbsp;</td>';
				$html .= '<td style="width: 120px;">' . $fItem->getNormalResultFemale() . '&nbsp;</td>';
				$html .= '</tr>';
				$count1++;

			} else {
				//has more than one result
				//group
				$html .= '<tr>';
				$html .= '<td class="group_header" style="width: 200px;">';
				$html .= $count . '. ' . $item->getLabAssignedItemOrdo()->getNameText();
				$html .= '</td>';
				$html .= '<td class="group_header" style="width: 100px;">&nbsp;</td>';
				$html .= '<td class="group_header" style="width: 65px;">&nbsp;</td>';
				$html .= '<td class="group_header" style="width: 120px;">&nbsp;</td>';
				$html .= '<td class="group_header" style="width: 120px;">&nbsp;</td>';
				$html .= '</tr>';
				//result item
				foreach ($labItemsFactorModel as $fItem) {
					$html .= '<tr>';
					if ($fItem->isNormal($person->getGender())) {
						$html .=   '<td style="width: 200px;">&nbsp;&nbsp;-&nbsp;' . $fItem->getNameText() . '</td>';
					} else {
						$html .=   '<td style="width: 200px;"><span style="color: red;">&nbsp;&nbsp;-&nbsp;' . $fItem->getNameText() . ' [*]</span></td>';
					}
					if ($isInput == true) {
						$html .=   '<td style="width: 100px;" onclick="editRow(\'' . $count1.'\')">';
					} else {
						$html .=   '<td style="width: 100px;" >';
					}
					$html .= '<span id="text_' . $count1 . '">' . $fItem->getResult() . '&nbsp;</span>';
					$html .= '<span id="edit_' . $count1 . '" style="display: none;"><input class="labItemResultInput" labresultid="' . $fItem->getLabResultId() . '" type="text" style="width: 90px;" value="' . $fItem->getResult() . '"/></span>';
					$html .= '</td>';
					$html .= '<td style="width: 65px;">' . $fItem->getUnit() . '&nbsp;</td>';
					$html .= '<td style="width: 120px;">' . $fItem->getNormalResultMale() . '&nbsp;</td>';
					$html .= '<td style="width: 120px;">' . $fItem->getNormalResultFemale() . '&nbsp;</td>';
					$html .= '</tr>';
					$count1++;

				}
			}


			$count++;
		}
		$html .= '</tbody>';
		$html .= '</table>';
		$html .= '</div>';

		return $html;
	}

	public function renderResultInput4Print($labAssignmentTicketId) {
		$labTicketObj = Celini::newOrdo('LabTicket');
		$listOfLabTicket = $labTicketObj->listLabTickets($labAssignmentTicketId, true);

		$labAssignmentTicketObj = Celini::newOrdo('LabAssignmentTicket');
		$labAssignmentTicketObj->setup($labAssignmentTicketId);
		$person = Celini::newORDO('Person', $labAssignmentTicketObj->get('patient_id'));
		$html = '';
		$html .= '<table cellspacing="0" cellpadding="0" style="width: 100%; margin-bottom: 10px;" class="grid">';
		$html .= '<thead>
                    <tr>
                        <th style="width: 20%;">'. L10nHelper::_('Group') . '</th>
                        <th style="width: 20%;">'. L10nHelper::_('Element') . '</th>
                        <th style="width: 15%;">'. L10nHelper::_('Result') . '</th>
                        <th style="width: 15%;">'. L10nHelper::_('Unit') . '</th>
                        <th style="width: 15%;">'. L10nHelper::_('Normal(M)') . '</th>
                        <th style="width: 15%;">'. L10nHelper::_('Normal(F)') . '</th>
                    </tr>
                </thead>';
		$countGroup = 1;

		foreach ($listOfLabTicket as $labTicket) {
			$labResultModel = $this->labResultEngine->getLabModel($labTicket['lab_ticket_id']);
			$labAssignedItemsModel = $labResultModel->getLabAssignedItemsModel();
			$labTicketInfo = $labResultModel->getLabTicketOrdo()->getLabTicket();
			$html .=   '<tr><td colspan="6">'. $countGroup . '. ' . $labTicketInfo['name'] . '&nbsp;</td></tr>';
			$countGroup++;
			$count = 1;
			$count1 = 1;
			foreach ($labAssignedItemsModel as $item) {
				//group

				//result item

				$labItemsFactorModel = $item->getLabItemsFactorModel();
				if (count($labItemsFactorModel) == 1) {
					//reindex
					$arr = array_values($labItemsFactorModel);
					$fItem = $arr[0];
					$html .= '<tr>';
					$html .= '<td>&nbsp;</td>';
					if ($fItem->isNormal($person->getGender())) {
						$html .=   '<td style="width: 200px;">' . $count . '. ' . $item->getLabAssignedItemOrdo()->getNameText() . '</td>';
					} else {
						$html .=   '<td style="width: 200px;"><span style="color: red;">' . $count . '. ' . $item->getLabAssignedItemOrdo()->getNameText() . '[*]</span></td>';
					}
					$html .=   '<td style="width: 100px;" >';
					$html .= '<span >' . $fItem->getResult() . '&nbsp;</span>';
					$html .= '</td>';
					$html .= '<td style="width: 60px;">' . $fItem->getUnit() . '&nbsp;</td>';
					$html .= '<td style="width: 120px;">' . $fItem->getNormalResultMale() . '&nbsp;</td>';
					$html .= '<td style="width: 120px;">' . $fItem->getNormalResultFemale() . '&nbsp;</td>';
					$html .= '</tr>';
				} else {
					$html .= '<tr><td>&nbsp;</td><td colspan="5"><span>' .
					$count . '. ' . $item->getLabAssignedItemOrdo()->getNameText().
                '</span></td></tr>';
					foreach ($labItemsFactorModel as $fItem) {
						$html .= '<tr><td>&nbsp;</td>';
						if ($fItem->isNormal($person->getGender())) {
							$html .=   '<td style="width: 200px;">&nbsp;&nbsp;-&nbsp;' . $fItem->getNameText() . '</td>';
						} else {
							$html .=   '<td style="width: 200px;"><span style="color: red;">&nbsp;&nbsp;-&nbsp;' . $fItem->getNameText() . '[*]</span></td>';

						}
						$html .=   '<td style="width: 100px;" >';
						$html .= '<span >' . $fItem->getResult() . '&nbsp;</span>';
						$html .= '</td>';
						$html .= '<td style="width: 60px;">' . $fItem->getUnit() . '&nbsp;</td>';
						$html .= '<td style="width: 120px;">' . $fItem->getNormalResultMale() . '&nbsp;</td>';
						$html .= '<td style="width: 120px;">' . $fItem->getNormalResultFemale() . '&nbsp;</td>';
						$html .= '</tr>';
					}
				}

				$count++;
			}
		}
		$html .= '</table>';

		return $html;
	}

	public function renderAssignmentInputResultPage($labAssignmentTicketId, $selectedLabTicketId) {
		$result =  array(
			'meta'          =>  '',
			'resultContent' =>  ''
		);
		$labResultModel = $this->labResultEngine->getLabModel($selectedLabTicketId);

		$result['meta'] = $this->renderMetaData($labAssignmentTicketId, $labResultModel);
		$result['resultContent'] = $this->renderResultInput($labAssignmentTicketId, $labResultModel, true);
		return $result;
	}

	public function renderAssignmentResultPage4View($labAssignmentTicketId, $selectedLabTicketId) {
		$result =  array(
                    'meta'          =>  '',
                    'resultContent' =>  ''
                    );
                    $labResultModel = $this->labResultEngine->getLabModel($selectedLabTicketId);

                    $result['meta'] = $this->renderMetaData4View($labAssignmentTicketId, $labResultModel);
                    $result['resultContent'] = $this->renderResultInput($labAssignmentTicketId, $labResultModel, false);
                    return $result;

	}
	public function renderAssignmentResultPage4Print($labAssignmentTicketId) {
		$result =  array(
                    'meta'          =>  '',
                    'resultContent' =>  ''
                    );
                    
                    $result['meta'] = $this->renderMetaData4Print($labAssignmentTicketId);
                    $result['resultContent'] = $this->renderResultInput4Print($labAssignmentTicketId);
                    return $result;

	}


}
?>